package com.yuanye.gwcit.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Environment;

import com.google.gson.Gson;
import com.yuanye.gwcit.GW;
import com.yuanye.gwcit.bean.CfgPcba;
import com.yuanye.yeecho.utils.YY;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.nio.charset.StandardCharsets;
import java.util.Enumeration;

public class GT {

    public static SharedPreferences getSP(Context context){
        return context.getSharedPreferences(GW.SHARE_PREF_NAME, Context.MODE_PRIVATE);
    }

    public static CfgPcba loadJsonFromFile(String filePath) throws Exception{
        BufferedReader reader = null;
        reader = new BufferedReader(new FileReader(filePath));
        StringBuilder stringBuilder = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            stringBuilder.append(line).append('\n');
        }
        reader.close();

        String jsonString = stringBuilder.toString().trim();
        Gson gson = new Gson();
        CfgPcba cfgPcba = gson.fromJson(jsonString, CfgPcba.class);
        return cfgPcba;
    }

    public static String getBoardSn() {
        return YY.execShell("vendor_storage r -i 5 -t string");
    }

    public static String getLocalSn() {
        return YY.execShell("vendor_storage r -i 5 -t string");
    }

    public static String getMacAddress() {
        return YY.execShell("cat /sys/class/net/eth0/address");
    }

    public static String getAppVersion(Context context){
        String currentVersionName = "";
        try {
            PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
//            int currentVersionCode = packageInfo.versionCode;
            currentVersionName = packageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return currentVersionName;
    }

    public static String getIpAddress(String netName){
        try {
            // 获取本地设备的所有网络接口
            Enumeration<NetworkInterface> enumerationNi = NetworkInterface.getNetworkInterfaces();
            while (enumerationNi.hasMoreElements()) {
                NetworkInterface networkInterface = enumerationNi.nextElement();
                String interfaceName = networkInterface.getDisplayName();
                // 如果是有线网卡
                if (interfaceName.equals(netName)) {
                    Enumeration<InetAddress> enumIpAddr = networkInterface.getInetAddresses();
                    while (enumIpAddr.hasMoreElements()) {
                        // 返回枚举集合中的下一个IP地址信息
                        InetAddress inetAddress = enumIpAddr.nextElement();
                        // 不是回环地址，并且是ipv4的地址
                        if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
                            return inetAddress.getHostAddress();
                        }
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getLogString(String logFileName) {
        String logContent = "未找到测试log";
        File file = new File(Environment.getExternalStorageDirectory().getPath(), logFileName);
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream(file);
            int size = inputStream.available();
            byte[] buffer = new byte[size];
            inputStream.read(buffer);
            inputStream.close();
            logContent = new String(buffer, StandardCharsets.UTF_8);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return logContent;
    }

}
